package mapper.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ReflectionUtils;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

@Slf4j
public class ClassLoaderUtil {
    public static ClassLoader getClassLoader(String url) throws NoSuchMethodException, MalformedURLException, InvocationTargetException, IllegalAccessException {
        return getClassLoader(new File(url));
    }

    public static ClassLoader getClassLoader(File jarFile) throws MalformedURLException {
        Method method = ReflectionUtils.findMethod(URLClassLoader.class, "addURL", URL.class);
        URLClassLoader classLoader = new URLClassLoader(new URL[]{jarFile.toURI().toURL()}, ClassLoader.getSystemClassLoader());
        return classLoader;
    }
}